package com.lemner.ke.fragment;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.lemner.ke.R;
import com.lemner.ke.adapter.VideoTabRvAdapter;
import com.lemner.ke.base.BaseFragment;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.BaseObjectBean;
import com.lemner.ke.base.BasePageBean;
import com.lemner.ke.bean.VideoRvBean;
import com.lemner.ke.bean.VideoTabRvBean;
import com.lemner.ke.model.knowledge.VideoTabRvModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：张恺
 * <p>
 * 时间：2018/9/20
 * <p>
 * 类描述：视频详情-视频
 */

public class VideoTabFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private VideoTabRvAdapter tabRvAdapter;
    private VideoRvBean videoRvBean;
    private String ids = "";
    private int page = 1;
    private boolean hasNextPage;
    private int lastVisibleItemPosition;

    @Override
    public int getLayoutFile() {
        return R.layout.fragment_video_tab;
    }

    @Override
    public void initSetting() {

    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.rv_videodetail);
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        Bundle bundle = getArguments();
        VideoRvBean videoRvBean = (VideoRvBean) bundle.getSerializable("bean");
        if (videoRvBean != null) {
            ids = videoRvBean.getIds();
        }
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        tabRvAdapter = new VideoTabRvAdapter(getContext());
        recyclerView.setAdapter(tabRvAdapter);
        new VideoTabRvModel().getVideoListByIds(new BaseListener<BaseObjectBean<BasePageBean<VideoTabRvBean>>>() {

            @Override
            public void onResponse(BaseObjectBean<BasePageBean<VideoTabRvBean>> basePageBeanBaseObjectBean) {
                BasePageBean<VideoTabRvBean> baseBean = basePageBeanBaseObjectBean.getData();
                hasNextPage = basePageBeanBaseObjectBean.getData().getHasNextPage();
                List<VideoTabRvBean> list = baseBean.getList();
                tabRvAdapter.setList(list);
            }

            @Override
            public void onFail(String msg) {

            }
        }, page, ids, "createtime", "desc");

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING&& lastVisibleItemPosition + 1 == tabRvAdapter.getItemCount()) {
                    if (hasNextPage) {
                        page++;
                        new VideoTabRvModel().getVideoListByIds(new BaseListener<BaseObjectBean<BasePageBean<VideoTabRvBean>>>() {

                            @Override
                            public void onResponse(BaseObjectBean<BasePageBean<VideoTabRvBean>> bean) {
                                BasePageBean<VideoTabRvBean> baseBean = bean.getData();
                                List<VideoTabRvBean> list = baseBean.getList();
                                if (hasNextPage)
                                tabRvAdapter.addList(list);
                                hasNextPage = bean.getData().getHasNextPage();
                            }

                            @Override
                            public void onFail(String msg) {

                            }
                        }, page, ids, "createtime", "desc");

                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition = tabRvAdapter.getItemCount() - 1;
            }
        });
    }

    @Override
    public void onClick(View v) {

    }
}
